前言:
知道怎麼使用 Firebase 的資料後
可以開始把辨識的程式碼銜接上去
辨識程式碼:
import time
import sys
import numpy as np
from firebase import firebase
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.preprocessing import image
key = "XXXXXXXXXXXXXXXXXXXXXXXXX"
authentication = firebase.FirebaseAuthentication(key, 'XXXXXXX@gmail.com')
firebase.authentication = authentication
user = authentication.get_user()
firebase = firebase.FirebaseApplication('https://XXXXXXXXXXXXXXXX.firebaseio.com/', authentication=authentication)
resultAcc = firebase.get('/dogAcc', '')
print(resultAcc)
resultName = firebase.get('/dogName', '')
print(resultName)
# 從參數讀取圖檔路徑
files = ["dog.jpg"]
print(files)
print("=============================")
# 載入訓練好的模型
net = load_model('model-resnet50-final-11.h5')
cls_list = [...]
while True:
time.sleep(4)
# 辨識每一張圖
if firebase.get('/DetectBool', 'Bool') == "False":
print("前端偵測開啟,開始辨識...")
time.sleep(8)
for f in files:
img = image.load_img(f, target_size=(300, 300))
if img is None:
continue
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
pred = net.predict(x)[0]
top_inds = pred.argsort()[::-1][:1]
print(f)
for i in top_inds:
print('準確率: {:.2%} {}'.format(pred[i], cls_list[i]))
firebase.put('/dogAcc','Acc' , '{:.2%}'.format(pred[i]))
firebase.put('/dogName','Name' , '{}'.format(cls_list[i]))
firebase.put('/DetectBool', 'Bool', 'True')
#firebase.delete("/dogDetail",'AC')
else:
print("前端偵測關閉中...")